package com.financing.wap.activities.impl;


import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.financing.wap.activities.ActivitiesBusinessObject;
import com.financing.wap.dto.ActivitesDTO;
import com.financing.wap.dto.ActivitieGiftDTO;
import com.financing.wap.dto.UserDTO;
import com.financing.wap.service.IAccountService;
import com.financing.wap.service.IActivitesService;
import com.financing.wap.service.IActivitieGiftService;
import com.financing.wap.service.IUserService;
import com.financing.wap.util.Constants;

@Service("activitiesYLYXHDImpl")
public class ActivitiesYLYXHDImpl extends ActivitiesBusinessObject{

	protected Logger log =Logger.getLogger(this.getClass());
	//活动来源 2016 元宵活动  Code
	public final static String activitiesCode ="cd500";
	
	//活动来源 2016 元宵活动  默认分享人最多可领红包数
	public final static int activitiesCount =15;
	
	@Autowired
	private IActivitesService activitiesServiceImpl;
	
	@Autowired
	private IAccountService accountService;

	@Autowired
	private IUserService userService;
	
	@Autowired
	private IActivitieGiftService activitieGiftServiceImpl;
	/** 
	 * 2016 元宵红包活动
	 * @param object
	 */
	@SuppressWarnings("unchecked")
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public void handleYLYXHD(Object object) throws Exception{
		//因为红包是有额度的，多线程并发时需要同步处理，避免超红包额度，避免超红包重复领取
		synchronized (ActivitiesYLYXHDImpl.class) {
			HashMap<String,Object> map =(HashMap<String, Object>)object;
			UserDTO userDto = (UserDTO) map.get("faUser");
			
			//查询活动
			ActivitesDTO activitesDTO = activitiesServiceImpl.searchActivitesByCode(activitiesCode);
			if(activitesDTO == null){
				log.info("handleYLYXHD activitesDTO is null !!!");
				return;
			}
			
			//查询用户详细信息
			UserDTO faUserDTO = userService.queryUserById(userDto.getId());
			if(!"1".equals(faUserDTO.getRole()) &&  !"99".equals(faUserDTO.getRole()) && !"98".equals(faUserDTO.getRole())){
				log.info("handleYLYXHD faUserDTO is auth ok !!!");
				//已经认证通过  -不参加活动
				return;
			}
			
			//判断是否领取
			ActivitieGiftDTO activitieGiftDTO  = new ActivitieGiftDTO();
			activitieGiftDTO.setFaUserId(faUserDTO.getId());
			activitieGiftDTO.setActiviesCode(activitiesCode);
			activitieGiftDTO.setaSource(Constants.ActivitieGiftDTO.SOURCE_REGISTER);
			List<ActivitieGiftDTO> listLoginUser = activitieGiftServiceImpl.queryGifts(activitieGiftDTO);
			if((null==listLoginUser || listLoginUser.size() <=0)){
				log.info("handleYLYXHD addActivitesGif aSource 01 !!!");
				//登录人
				accountService.addActivitesGif(activitesDTO, userDto.getId(), null, activitesDTO.getActiviesAmount(), "01", Constants.ActivitieGiftDTO.SOURCE_REGISTER,Constants.ActivitieGiftDTO.STATUS_IN_EFFECT);
			}

		}
	}
	/** 
	 * 提交认证
	 * @param object
	 */
	@SuppressWarnings("unchecked")
	@Transactional(rollbackFor={Exception.class,RuntimeException.class})
	public void handleFaUserSubmitAuth(Object object) throws Exception{
		//因为红包是有额度的，多线程并发时需要同步处理，避免超红包额度，避免超红包重复领取
		synchronized (ActivitiesYLYXHDImpl.class) {
			HashMap<String,Object> map =(HashMap<String, Object>)object;
			String userId = (String) map.get("userId");
			String shareUserId = null;
			try {
				shareUserId = (String) map.get("shareUserId");
			} catch (Exception e) {
				log.error("handleFaUserSubmitAuth shareUserId is error!!!");
				
			}
			if(StringUtils.isBlank(shareUserId)){
				log.info("handleFaUserSubmitAuth shareUserId is null !!!");
				return;
			}
			if(shareUserId.equals(userId)){
				log.info("handleFaUserSubmitAuth userId shareUserId same !!!");
				return;
			}
			
			//查询活动
			ActivitesDTO activitesDTO = activitiesServiceImpl.searchActivitesByCode(activitiesCode);
			if(activitesDTO == null){
				log.info("handleFaUserSubmitAuth activitesDTO is null !!!");
				return;
			}
			
			//查询用户详细信息
			UserDTO faUserDTO = userService.queryUserById(userId);
			if(!Constants.USER_ROLE_AUDIT.equals(faUserDTO.getRole())){
				log.info("handleFaUserSubmitAuth faUserDTO is auth ok !!!");
				//不是认证中  -不参加活动
				return;
			}
			
			//判断分享人是否领取
			ActivitieGiftDTO activitieGiftDTO  = new ActivitieGiftDTO();
			activitieGiftDTO.setActiviesCode(activitiesCode);
			activitieGiftDTO.setFaShareUserId(userId);
			activitieGiftDTO.setaSource(Constants.ActivitieGiftDTO.SOURCE_SHARE);
			List<ActivitieGiftDTO> listShareUser = activitieGiftServiceImpl.queryGifts(activitieGiftDTO);
			for (ActivitieGiftDTO bean:listShareUser) {
				//用户  分享人是否已经被领取  01:生效中，02：已生效，，04：提现  
				if(Constants.ActivitieGiftDTO.STATUS_IN_EFFECT.equals(bean.getaStatus()) || Constants.ActivitieGiftDTO.STATUS_EFFECT.equals(bean.getaStatus())
						|| Constants.ActivitieGiftDTO.STATUS_WITHDRAWALS.equals(bean.getaStatus())){
					return;
				}
			}
			
			//判断分享人是否超出人数限制
			//02：已生效，，04：提现  才算
			activitieGiftDTO.setFaUserId(shareUserId);
			activitieGiftDTO.setFaShareUserId(null);
			activitieGiftDTO.setaSource(Constants.ActivitieGiftDTO.SOURCE_SHARE);
			List<ActivitieGiftDTO> listShareUserCount = activitieGiftServiceImpl.queryGifts(activitieGiftDTO);
			int listShareUserCountSize = 0;
			for (ActivitieGiftDTO bean:listShareUserCount) {
				//02：已生效，，04：提现  才算
				if(Constants.ActivitieGiftDTO.STATUS_EFFECT.equals(bean.getaStatus())
						|| Constants.ActivitieGiftDTO.STATUS_WITHDRAWALS.equals(bean.getaStatus())){
					listShareUserCountSize ++;
				}
			}
			log.info("handleFaUserSubmitAuth listShareUserCountSize:"+listShareUserCountSize);
			if(listShareUserCount == null || listShareUserCountSize < (activitesDTO.getActiviesCount()==null?activitiesCount:activitesDTO.getActiviesCount())){
				log.info("handleYLYXHD addActivitesGif aSource 02&04 <=15!!!");
				//分享人
				accountService.addActivitesGif(activitesDTO, shareUserId, userId, activitesDTO.getActiviesAmountOlduser(), "01", Constants.ActivitieGiftDTO.SOURCE_SHARE,Constants.ActivitieGiftDTO.STATUS_IN_EFFECT);
			}

		}
	}
}
